package com.chanjiedata.changbs.module.count;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.ProgressAdapter;
import com.chanjiedata.changbs.base.BaseFragment;
import com.chanjiedata.changbs.bean.ProgressBean;
import com.chanjiedata.changbs.dialog.SelectMonthDialog;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Statistics;
import com.google.protobuf.InvalidProtocolBufferException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import kale.ui.view.dialog.EasyDialog;

/**
 * Created by Administrator on 2018/2/2 0002.
 */

public class CountSixthFragment extends BaseFragment {


    Unbinder unbinder;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.tv_time_search)
    TextView tvTimeSearch;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private ProgressAdapter mAdapter;
    private List<ProgressBean> datas = new ArrayList<>();
    private String search_month = new SimpleDateFormat("yyyy-MM").format(new Date());

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_count_sixth;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initView() {
        tvTimeSearch.setText(search_month);
        tvTimeSearch.setOnClickListener(v -> {
            SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(mContext);
            builder.setOnDialogClickListener((view, month) -> {
                search_month = month;
                tvTimeSearch.setText(search_month);
                initData();
            });
            EasyDialog build = builder.build();
            build.setCancelable(true);
            build.show(getChildFragmentManager());
        });

        initAdapter();
        initData();
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new ProgressAdapter(mContext, datas);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
        });
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.setEmptyView(R.layout.recycle_empty);
    }


    @Override
    protected void init() {

    }


    private void initData() {
        Statistics.get_total_trading_month.Builder builder = Statistics.get_total_trading_month.newBuilder();
        builder.setMonth(search_month);
        NetworkManager.INSTANCE.post(Apis.get_merchant_trading_month, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                datas.clear();
                double totalValue = 0.0;
                List<Statistics.basis> list = Statistics.get_total_trading_month.parseFrom(response).getAmountList();
                Log.e("wang",list.toString());
                for (int i = 0; i < list.size(); i++) {
                    totalValue = totalValue + Double.valueOf(list.get(i).getNum());
                }
                if (totalValue == 0) {
                    for (int i = 0; i < list.size(); i++) {
                        ProgressBean dataBean = new ProgressBean();
                        double progress = 0;
                        dataBean.setProgress((int) progress);
                        dataBean.setName(list.get(i).getName());
                        dataBean.setNumber(list.get(i).getNum());
                        dataBean.setPosition(i);
                        datas.add(dataBean);
                    }
                } else {
                    for (int i = 0; i < list.size(); i++) {
                        ProgressBean dataBean = new ProgressBean();
                        double progress = ((Double.valueOf(list.get(i).getNum()) / totalValue) * 100);
                        dataBean.setName(list.get(i).getName());
                        dataBean.setProgress((int) progress);
                        dataBean.setNumber(list.get(i).getNum());
                        dataBean.setPosition(i);
                        datas.add(dataBean);
                    }
                }
                mAdapter.replaceData(datas);
                mAdapter.loadMoreEnd();
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }

    public void reLoadData() {
       initData();
    }

}
